/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package boundary_package;

import Entitys.Persona;
import Helpers.RegexFormatter;
import Helpers.RegularExpression;
import control_package.LoginControl;

import javax.swing.JOptionPane;

/**
 *
 * @author Administrador
 */
public class LoginGui extends javax.swing.JPanel {
     private Persona pers;  
     public static Persona persL;
     private LoginControl logCon; 
     
     private MainFrame frame;
    /**
     * Creates new form LoginGui
     */
    public LoginGui(MainFrame frame) {
        this.setSize(350,175);
        initComponents();
        this.frame=frame;
        new RegexFormatter(RegularExpression.NATURAL_NUMBERS).install(IdentificationTf);
        this.IdentificationTf.getCursor();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        PasswordTf = new javax.swing.JPasswordField();
        AceptarButton = new javax.swing.JButton();
        Contraseña = new javax.swing.JLabel();
        IdentificationTf = new javax.swing.JFormattedTextField();

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "INGRESAR", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Verdana", 0, 18))); // NOI18N

        jLabel1.setFont(new java.awt.Font("Verdana", 0, 24)); // NOI18N
        jLabel1.setText("Identificación");

        PasswordTf.setFont(new java.awt.Font("Verdana", 0, 24)); // NOI18N
        PasswordTf.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                PasswordTfKeyPressed(evt);
            }
        });

        AceptarButton.setFont(new java.awt.Font("Verdana", 0, 18)); // NOI18N
        AceptarButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Icons/Copia de icon_check.png"))); // NOI18N
        AceptarButton.setText("ACEPTAR");
        AceptarButton.setBorder(new javax.swing.border.MatteBorder(null));
        AceptarButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                AceptarButtonActionPerformed(evt);
            }
        });

        Contraseña.setFont(new java.awt.Font("Verdana", 0, 24)); // NOI18N
        Contraseña.setText("Contraseña");

        IdentificationTf.setFont(new java.awt.Font("Verdana", 0, 24)); // NOI18N

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addComponent(Contraseña))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(IdentificationTf, javax.swing.GroupLayout.PREFERRED_SIZE, 396, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(PasswordTf, javax.swing.GroupLayout.PREFERRED_SIZE, 396, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(206, 206, 206)
                        .addComponent(AceptarButton, javax.swing.GroupLayout.PREFERRED_SIZE, 200, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(38, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(IdentificationTf, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(14, 14, 14)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(Contraseña)
                    .addComponent(PasswordTf, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 51, Short.MAX_VALUE)
                .addComponent(AceptarButton, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void AceptarButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_AceptarButtonActionPerformed
        if(this.IdentificationTf.getText().equals("")||this.PasswordTf.getText().equals("")){
            JOptionPane.showMessageDialog(null,"Campos vacíos", "MENSAJE", JOptionPane.OK_OPTION);  
            cleanFields();
        }else{
             pers=new Persona(new Long(Long.parseLong(this.IdentificationTf.getText())));
             persL=new Persona();
             logCon=new LoginControl(); 
             pers.setPassPersona(this.PasswordTf.getText());
             persL=logCon.getLooged(pers);//aqui esta el usuario logeado
             
             
            if(logCon.verifyLogin(pers).equals("Login Hecho")&&logCon.VerifyRol(persL)){
                frame.setToolBarAdmin(persL.getNombrePersona()); 
            }else if(logCon.verifyLogin(pers).equals("Login Hecho")&&!logCon.VerifyRol(persL)){
                frame.setToolBarSeller(persL.getNombrePersona());
            }else{
                JOptionPane.showMessageDialog(null,logCon.verifyLogin(pers) , "MENSAJE", JOptionPane.OK_OPTION);  
                 cleanFields();
            }
        }
    }//GEN-LAST:event_AceptarButtonActionPerformed

    private void PasswordTfKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_PasswordTfKeyPressed
        if(evt.getKeyCode()==10){
            if(this.IdentificationTf.getText().equals("")||this.PasswordTf.getText().equals("")){
                JOptionPane.showMessageDialog(null,"Campos vacíos", "MENSAJE", JOptionPane.OK_OPTION);  
                cleanFields();
            }else{
                pers=new Persona(new Long(Long.parseLong(this.IdentificationTf.getText())));
                persL=new Persona();
                logCon=new LoginControl(); 
                pers.setPassPersona(this.PasswordTf.getText());
                persL=logCon.getLooged(pers);//aqui esta el usuario logeado
               
            if(logCon.verifyLogin(pers).equals("Login Hecho")&&logCon.VerifyRol(persL)){
                frame.setToolBarAdmin(persL.getNombrePersona()); 
            }else if(logCon.verifyLogin(pers).equals("Login Hecho")&&!logCon.VerifyRol(persL)){
                frame.setToolBarSeller(persL.getNombrePersona());
            }else{
                JOptionPane.showMessageDialog(null,logCon.verifyLogin(pers) , "MENSAJE", JOptionPane.OK_OPTION);  
                 cleanFields();
            }
            }
        }
    }//GEN-LAST:event_PasswordTfKeyPressed

    public void cleanFields(){
     this.IdentificationTf.setText("");
     this.PasswordTf.setText("");
    
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton AceptarButton;
    private javax.swing.JLabel Contraseña;
    private javax.swing.JFormattedTextField IdentificationTf;
    private javax.swing.JPasswordField PasswordTf;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    // End of variables declaration//GEN-END:variables
}
